# 数据处理主程序入口
import time
from as400_OCR_discern import compute_as400_OCR
from as400_OCR_discern.serial_number import serial
from OCR_RMC import out_ticket_scan, OCR_scan
import queue

import gevent
from gevent import monkey
monkey.patch_all()

# 读码对比程序启动
class Video_qr():
    def __init__(self, sinOut):
        self.maxCODE = {}
        self.minCODE = []
        self.start_time = None          # 开始时间
        self.end_time = None            # 结束时间
        self.plastic_box_num = None     # 相机识别到的胶箱数
        self.OCR_REQ = None             # 货品数量
        self.H_OCR = queue.Queue()
        self.sinOUT = sinOut            # 信号

    # 进入高拍仪与OCR数据获取
    def GET_OCR_H(self):
        # OCR对象
        Hobj = out_ticket_scan.get_H_str(self.sinOUT)
        # 高拍仪对象
        OCRobj = OCR_scan.OCR_scan_bar(self.sinOUT)

        gevent.joinall([
            gevent.spawn(OCRobj.start),
            gevent.spawn(Hobj.start),
        ])

        # 从通道获取数据  queue  Q
        H_data_dict = {}
        while not Hobj.Hq.empty():
            H_bar = Hobj.Hq.get()
            H_data_dict.update(H_bar)
        while not OCRobj.OCRq.empty():
            OCR_bar_list = OCRobj.OCRq.get()
        return H_data_dict, OCR_bar_list

    # 主程序启动
    def start(self):
        self.sinOUT.emit("result&程序开始运行")
        self.start_time = time.time()               # 开始测试时间
        serial_number = serial()                    # 生成一个序列编号  做为唯码一  Y1DTTM
        self.sinOUT.emit("result&唯一码为: %s" % serial_number)

        # 进入高拍仪和OCR扫描数据
        H_data_dict, OCR_bar_list = self.GET_OCR_H()

        # OCR扫描数据
        self.minCODE = OCR_bar_list
        # 高拍仪数据集合
        self.maxCODE = H_data_dict

        # print("OCR条码:", self.minCODE)
        # print("高拍仪数据:", self.maxCODE)

        # self.sinOUT.emit("result&获取到RMC条码: %s" % str(self.minCODE))
        # self.sinOUT.emit("result&获取到出张票数据: %s" % str(self.maxCODE))

        # 扫描结束时间
        self.end_time = time.time()

        # 点数器清点实物胶箱总和  0100011
        self.ocr_read_box_sum = len(OCR_bar_list)

        if self.minCODE and self.maxCODE and self.ocr_read_box_sum:   # 同时需要获取大码与小码的数据  一张大票正常识别   self.plastic_box_num: 相机识别到胶箱数量 , 及货品数量都正常获取
            self.sinOUT.emit("result&进入数据处理!")
            OCR_obj = compute_as400_OCR.as400_OCR(self.minCODE, self.maxCODE, self.ocr_read_box_sum, self.start_time, self.end_time, self.sinOUT, serial_number)
            OCR_obj.OCR_AS400()         #  扫描的RMC条码数据与as400数据对比
        else:
            print("扫描异常")
            self.sinOUT.emit("扫描异常!")
        self.sinOUT.emit("result&程序运行结束")
        self.sinOUT.emit("end&")


if __name__=='__main__':
    import tkinter
    from tkinter import font

    def call(event):
        if event.keysym == "Prior":  # 打印按下的键值
            text_str.config(state=tkinter.NORMAL)
            text_str.delete(1.0, tkinter.END)
            text_str.insert(tkinter.INSERT, "开始运行: %s" % (time.strftime("%Y-%m-%d / %H:%M:%S", time.localtime())), "tag_1")
            text_str.update()
            obj = Video_qr()
            obj.start()  # 程序运行
            text_str.insert(tkinter.END, "\n结束运行: %s" % (time.strftime("%Y-%m-%d / %H:%M:%S", time.localtime())), "tag_1")
            text_str.update()
            text_str.config(state=tkinter.DISABLED)

    win = tkinter.Tk()
    win.title("OCR_RMC")
    text_str = tkinter.Text(win, width=50, height=6, state=tkinter.DISABLED)
    ft = font.Font(family='微软雅黑', size=15)              ###有很多参数
    text_str.tag_config("tag_1", foreground="red", font=ft)

    win.bind("<Key>", call)
    text_str.pack()
    win.focus_get()
    win.mainloop()





